<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('订单')}:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_select('row[order_id]', $orderList, null, ['class'=>'form-control selectpicker', 'data-rule'=>'required', 'id'=>'order-select'])}
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('型号')}:</label>
        <div class="col-xs-12 col-sm-8">
            <select name="row[model_id]" class="form-control selectpicker" data-rule="required" id="model-select">
                <option value="">请先选择订单</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('工序')}:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_select('row[process_id]', $processList, null, ['class'=>'form-control selectpicker', 'data-rule'=>'required', 'id'=>'process-select'])}
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('员工')}:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_select('row[user_id]', $userList, null, ['class'=>'form-control selectpicker', 'data-rule'=>'required'])}
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('工作类型')}:</label>
        <div class="col-xs-12 col-sm-8">
            <select name="row[work_type]" class="form-control selectpicker" id="work-type-select">
                <option value="piece">计件</option>
                <option value="time">计时</option>
            </select>
        </div>
    </div>
    <div class="form-group" id="quantity-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('分配数量')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-quantity" data-rule="required;number" class="form-control" name="row[quantity]" type="number" min="1" value="">
            <span class="help-block">最大可分配数量：<span id="max-quantity">0</span></span>
        </div>
    </div>
    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
            <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
        </div>
    </div>
</form>

<script>
    $(document).ready(function() {
        function toggleQuantityField() {
            var workType = $('#work-type-select').val();
            if (workType === 'time') {
                // 计时模式：完全移除数量字段
                $('#quantity-group').remove();
            } else {
                // 计件模式：显示数量字段，添加验证规则
                $('#quantity-group').show();
                $('#c-quantity').attr('data-rule', 'required;number').val('');
            }
            
            // 重新初始化表单验证
            if (typeof $("#add-form").data('validator') !== 'undefined') {
                $("#add-form").data('validator').destroy();
            }
            $("#add-form").validator();
        }

        // 页面加载时初始化
        toggleQuantityField();

        // 工作类型改变时切换
        $('#work-type-select').on('change', function() {
            toggleQuantityField();
        });
        
        // 表单提交前处理
        $('#add-form').on('submit', function(e) {
            var workType = $('#work-type-select').val();
            if (workType === 'time') {
                // 计时模式下确保数量为1（后端会设置为0）
                $('#c-quantity').val('1');
            }
        });
    });
</script>